package com.audaque.springboot.foshanupload.web.webdemo.controller;

import com.audaque.springboot.foshanupload.core.component.ModelMapperConvertor;
import com.audaque.springboot.foshanupload.log.anno.WebLogAnno;
import com.audaque.springboot.foshanupload.web.anno.RAnno;
import com.audaque.springboot.foshanupload.web.webdemo.model.dto.Covert1Dto;
import com.audaque.springboot.foshanupload.web.webdemo.model.req.Covert11Req;
import com.audaque.springboot.foshanupload.web.webdemo.model.req.Covert1Req;
import com.audaque.springboot.foshanupload.web.webdemo.model.req.ScoreReq;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zgb
 * @desc 只复制非null属性，只匹配属性名称，与属性类型无关，会忽略不同的字段且不会报错
 * @date 2023-03-07 21:37:22
 */

@Slf4j
@RestController
@RequestMapping("modelMapperConvert")
public class ModelMapperConvertController {
    @Autowired
    private ModelMapperConvertor modelMapperConvertor;


    @RAnno
    @WebLogAnno
    @PostMapping("/qo2dto")
    public Covert1Dto qo2dto(@RequestBody  Covert1Req covert1Req) throws Exception {
        //只复制非null属性，只匹配属性名称，与属性类型无关
        Covert1Dto convert = modelMapperConvertor.convert(covert1Req, Covert1Dto.class);
        return convert;
    }


    @RAnno
    @WebLogAnno
    @PostMapping("/qoList2dtoList")
    public List<Covert1Dto> qoList2dtoList(@RequestBody List<Covert1Req> covert1ReqList) throws Exception {
        List<Covert1Dto> convert = modelMapperConvertor.convert(covert1ReqList, Covert1Dto.class);
        return convert;
    }





    @RAnno
    @WebLogAnno
    @PostMapping("/copyset")
    public Covert1Req copyset(@RequestBody  Covert1Req covert1Req) throws Exception {
        Covert1Req covert1Req1=new Covert1Req();
        covert1Req1.setId(11L);
        covert1Req1.setSubjectName("11");
        ScoreReq scoreReq = new ScoreReq();
        scoreReq.setId(11L);
        scoreReq.setChinese(11);
        covert1Req1.setScoreReq(scoreReq);
        covert1Req1.setDate(new Date());
        List<Covert11Req> covert11List=new ArrayList<>();
        Covert11Req covert11Req = new Covert11Req();
        covert11Req.setId(11L);
        covert11Req.setDate(new Date());
        covert11List.add(covert11Req);
        covert1Req1.setCovert11List(covert11List);

        modelMapperConvertor.convert(covert1Req, covert1Req1);

        //测试深浅拷贝
        covert1Req.setId(22L);
        ScoreReq scoreReq1 = covert1Req.getScoreReq();
        if(scoreReq1!=null){
            scoreReq1.setChinese(22);
        }

        covert1Req.setDate(new Date());
        Covert11Req covert22Req = new Covert11Req();
        covert22Req.setId(22L);
        covert22Req.setDate(new Date());
        List<Covert11Req> covert11List1 = covert1Req.getCovert11List();
        covert11List1= ListUtils.emptyIfNull(covert11List1);
        covert11List1 .add(covert22Req);
        return covert1Req1;
    }




}
